package com.e9where.canpoint.customrecycler.recycler.onscroll;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.ViewGroup;

import com.e9where.canpoint.customrecycler.recycler.callback.SlideCallBcak;
import com.e9where.canpoint.customrecycler.recycler.adapter.CustomAdapter;
import com.e9where.canpoint.customrecycler.recycler.item.CustomFoot;
import com.e9where.canpoint.customrecycler.recycler.mode.SlideCallMode;
import com.e9where.canpoint.customrecycler.recycler.mode.SlideMode;

import static android.widget.AbsListView.OnScrollListener.SCROLL_STATE_FLING;
import static android.widget.AbsListView.OnScrollListener.SCROLL_STATE_IDLE;
import static android.widget.AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL;

/**
 * Created by Administrator on 2017/12/1.
 * 自定义滑动监听，用于进行上滑加载
 */

public class CustomOnScroll extends RecyclerView.OnScrollListener{

    private Context context;
    private SlideMode slideMode;
    private SlideCallBcak slideCallBcak;

    public void setSlideMode(SlideMode slideMode) {
        this.slideMode = slideMode;
    }

    //是否加载中
    private boolean is_Load;
    //是否不在滑动
    private boolean no_Scroll;
    //屏幕上显示的最后一个item的序列号
    private int lastVisibleItemPosition;
    //滑动是不是向下
    private boolean is_up;

    public void setIs_up(boolean is_up) {
        this.is_up = is_up;
    }

    private CustomFoot customFoot;
    private CustomAdapter customAdapter;


    public CustomOnScroll(Context context, SlideMode slideMode, SlideCallBcak slideCallBcak) {
        this.context = context;
        this.slideMode = slideMode;
        this.slideCallBcak = slideCallBcak;
    }

    public CustomOnScroll(Context context) {
        this.context = context;
    }

    //滚动的时候一直调用
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);

        if (slideCallBcak!=null)
        {
            no_Scroll=false;
            RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
            if (layoutManager instanceof GridLayoutManager)
            {
                lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
            }
            else if (layoutManager instanceof LinearLayoutManager)
            {
                lastVisibleItemPosition=((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
            }
            else if(layoutManager instanceof StaggeredGridLayoutManager)
            {
                StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
                int[] lastPositions = new int[staggeredGridLayoutManager.getSpanCount()];
                staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions);
                lastVisibleItemPosition = getLast(lastPositions);
            }
            else
            {
                throw new IllegalArgumentException("layoutManager can't null or layoutManager shi error");
            }
        }

    }

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        customAdapter =  (CustomAdapter)recyclerView.getAdapter();
        switch (newState)
        {
            //停止滚动
            case SCROLL_STATE_IDLE:
                isLoging(true);
                if (slideCallBcak!=null)
                    manageData(recyclerView);
                break;
            //正在滚动
            case SCROLL_STATE_TOUCH_SCROLL:
                isLoging(false);
                break;
            //惯性运动
            case SCROLL_STATE_FLING:
                isLoging(false);
                break;
        }
    }

    //可以在改方法设置滑动是否加载图片
    private void isLoging(boolean isLoad)
    {

    }

    //滑动停止处理数据
    private void manageData(RecyclerView recyclerView)
    {
        if (slideMode==SlideMode.NONE || slideMode==SlideMode.PULL_DOWN)
            return;
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        //可见数量
        int visibleItemCount = layoutManager.getChildCount();
        //总数量
        int totalItemCount = layoutManager.getItemCount();
        if (visibleItemCount>0&& this.lastVisibleItemPosition >=totalItemCount-1&&is_up)
        {
            if (is_Load)
                return;
            //是否滚动停止
            if (no_Scroll)
            {
                no_Scroll = !no_Scroll;
                return;
            }
            is_Load=true;
            addFoot();
            slideCallBcak.onSlideCallBcak(SlideCallMode.PULL_UP);
        }
    }

    //刷线开始 ，添加尾部
    private void addFoot()
    {
        if (customFoot==null)
        {
            customFoot=new CustomFoot(context);
            customFoot.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        }
        customAdapter.addFoot(customFoot);
        customFoot.onLoad();
    }




    //刷新结束 删除尾部
    public void onFinish()
    {
        if (customAdapter!=null)
        {
            if (customFoot==null)
                return;
            customFoot.onFinish();
            customAdapter.removeFoot(customFoot);
            is_Load=false;
            no_Scroll=true;
        }
    }

    //删除没有更多
    public void removeMore()
    {
        if (customAdapter!=null)
        {
            if (customFoot==null)
                return;
            customFoot.onFinish();
            customAdapter.removeFoot(customFoot);
        }
    }


    //没有更多
    public void onMore()
    {
        if (customFoot==null)
        {
            customFoot=new CustomFoot(context);
            customFoot.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        }
        customAdapter.addFoot(customFoot);
        customFoot.onMore();
    }


    private int getLast(int[] mPositions) {
        int last = mPositions[0];
        for (int value : mPositions) {
            if (value > last) {
                last = value;
            }
        }
        return last;
    }

}
